#!/bin/zsh

# glabal_github_proxy_url="https://github.moeyy.xyz/"
glabal_github_proxy_url="https://gh.xmly.dev/"

# 下载
file_downloader(){
  echo "[Debug] === file_downloader()  ==="
  file_download_url=$1
  echo "[Debug] file_download_url = $file_download_url"
  # 判断参数是否为空
  if [ ! -n "$2" ]; then
    echo "[Debug] 参数为空!"
    download_file_save_path="$HOME/Downloads"
    file_name=`echo $file_download_url | awk -F'/' '{print $NF}'`
    # file_name=`echo $file_download_url | rev | cut -d '/' -f 1 | rev`
    echo "[Debug] file_name = $file_name"
    save_file_path="$HOME/Downloads/$file_name"
    save_file_path="$2/$file_name"
  else
    echo "[Debug] 参数不为空!"
    save_file_path=$2
  fi
  echo "[Debug] save_file_path = $save_file_path"

  # 判断是否文件存在
  if [[ ! -f "$save_file_path" ]]; then
    echo "[Debug] 文件 $save_file_path 不存在！现在开始下载..."
    if [[  -x "$(which axel)" ]]; then
      echo "[Debug] 使用axel下载 $file_download_url"
      axel --output "$save_file_path" "$file_download_url"
    elif [[  -x "$(which wget)" ]]; then 
      echo "[Debug] 使用wget下载 $file_download_url"
      # wget https://evermeet.cx/ffmpeg/ffmpeg-113507-gf6b7b473d4.zip 
      # wget -c "$file_download_url"
      wget -c -O  "$save_file_path" "$file_download_url"
      # wget -b -c "$file_download_url" # 后台下载+断点续传
    else
      echo "[Debug] 使用curl下载 $file_download_url"
      cd $download_file_save_path
      curl -C -# -R -O -I "$file_download_url" 
      curl -R -O  "$file_download_url"  -o "$save_file_path" # 显示下载进度、断点下载
      # -O 将文件下载到当前目录下
      # -o 将文件 下载到 download 目录下，并重命名为 test.txt
      # -# 显示下载进度
      # -C 断点续传
      # [Linux 中三个下载工具-wget curl axel - 知乎](https://zhuanlan.zhihu.com/p/451426230)
    fi
  else 
    echo "[INFO] 文件 $save_file_path 存在！"
  fi
}

# NeatDownloadManager
file_downloader "${glabal_github_proxy_url}https://objects.githubusercontent.com/github-production-release-asset-2e65be/785072943/f6917430-1292-4848-a9fb-35c0b95484c0?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20240619%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240619T025024Z&X-Amz-Expires=300&X-Amz-Signature=61ca67d26766e6644c6873f9eb065d3dcebb62b3d80a5a19e3bc5a516cd5dddc&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=785072943&response-content-disposition=attachment%3B%20filename%3DNeatDMInstaller.dmg&response-content-type=application%2Foctet-stream"

# 阿里云盘
file_downloader "https://cdn.aliyundrive.net/downloads/apps/desktop/aDrive-4.16.0.dmg"
# VSCode
file_downloader "https://vscode.download.prss.microsoft.com/dbazure/download/stable/611f9bfce64f25108829dd295f54a6894e87339d/VSCode-darwin-universal.zip"
# git
file_downloader "https://zenlayer.dl.sourceforge.net/project/git-osx-installer/git-2.33.0-intel-universal-mavericks.dmg?viasf=1"
# wezterm
file_downloader "${glabal_github_proxy_url}https://github.com/wez/wezterm/releases/download/20240203-110809-5046fc22/WezTerm-macos-20240203-110809-5046fc22.zip"
